package com.sun.hunk.hibernate.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.sun.hunk.hibernate.domain.User;
import com.sun.hunk.hibernate.BaseHibernateDAO;
import com.sun.hunk.hibernate.domain.impl.UserImpl;
import com.sun.hunk.util.CommonUtil;

/**
 *
 * @author sunil
 */
public class UserDAO extends BaseHibernateDAO<UserImpl> {

    public UserDAO() {
        super(UserImpl.class);
    }

    public User getUser(String userName, String password) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("login_name", userName);
        map.put("password", password);
        List<User> list = find(map);
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }

    public boolean isLoginNameAvailable(String loginName) {
        UserDAO userDAO = new UserDAO();
        User user = userDAO.getByProperty("loginName", loginName);
        if (user == null) {
            return true;
        } else {
            return false;
        }
    }

    public List<User> findUsers(User user) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (CommonUtil.isNotEmpty(user.getFirstName())) {
            map.put("firstName", user.getFirstName());
        }
        if (CommonUtil.isNotEmpty(user.getLastName())) {
            map.put("lastName", user.getLastName());
        }
        if (CommonUtil.isNotEmpty(user.getEmail())) {
            map.put("email", user.getEmail());
        }
        return new UserDAO().find(map);
    }
}
